//
//  MJTableView.m
//  GoodEit
//
//  Created by ihope99 on 2017/8/10.
//  Copyright © 2017年 GZX. All rights reserved.
//

#import "MJTableView.h"
#import "MJRefresh.h"
#import "Masonry.h"



@interface MJTableView ()
@property (nonatomic,strong) UIView *norMessageView;
@property (nonatomic,strong) UIImageView *norImageView;
@property (nonatomic,strong) UILabel *norLabel;
@end

@implementation MJTableView


-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self CreatNorView];
        //自动更改透明度
        self.mj_header.automaticallyChangeAlpha = YES;
        //进入刷新状态
        //        [self.mj_header beginRefreshing];
        self.isAddFooter = YES;
        self.isAddHeader = YES;
        self.isNoMoreView = YES;
        self.isNoMoreData = YES;
        self.isRefreshClick = YES;
        self.cacheHeightDict = [NSMutableDictionary dictionary];
        self.norImage = [UIImage imageNamed:@"moren_wushe"];
        self.norText = @"暂无数据";

    }
    return self;
}
-(void)CreatNorView{
    self.norMessageView = [[UIView alloc]initWithFrame:CGRectMake(0,(self.frame.size.height - 200 - StatusBarAndNavH)/2, kScreenWidth, 200)];
    [self addSubview:self.norMessageView];
    self.norMessageView.hidden = YES;

    self.norImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
    self.norImageView.image = self.norImage;
    [self.norMessageView addSubview:self.norImageView];

    self.norLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    self.norLabel.text = @"暂无数据";
    self.norLabel.font = [UIFont systemFontOfSize:15.];
    self.norLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1];
    [self.norMessageView addSubview:self.norLabel];
    self.norLabel.textAlignment = NSTextAlignmentCenter;

    [self.norMessageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY).offset(-StatusBarAndNavH);
    }];

    [self.norImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.norImage.size);
        make.centerX.mas_equalTo(self.norMessageView);
        make.centerY.mas_equalTo(self.norMessageView.mas_centerY).offset(-20);
    }];

    [self.norLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.norImageView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.norMessageView);
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(20);
    }];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(norRefData)];
    [self.norMessageView addGestureRecognizer:tap];

}
-(void)setDel:(id)del{
    _del = del;
    self.mjDelegate = del;
    self.dataSource = del;
    self.delegate = del;
}
-(void)setIsRefreshClick:(BOOL)isRefreshClick{
    _isRefreshClick = isRefreshClick;
    self.norMessageView.userInteractionEnabled = isRefreshClick;
}
-(void)setNorText:(NSString *)norText{
    _norText = norText;
    self.norLabel.text = norText;
}
-(void)setNorImage:(UIImage *)norImage{
    _norImage = norImage;
    self.norImageView.image = norImage;
    [self.norImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.norImage.size);
        make.centerX.mas_equalTo(self.norMessageView);
        make.centerY.mas_equalTo(self.norMessageView.mas_centerY).offset(-20);
    }];
    [self.norMessageView layoutIfNeeded];

}

-(void)norRefData{
    self.isNoMoreData = NO;
    if (self.mjRefreshDataBlock) {
        self.mjRefreshDataBlock();
    }
    if (self.mjDelegate && [self.mjDelegate respondsToSelector:@selector(MjReloadAction)]) {
        [self.mjDelegate MjReloadAction];
    }
}
-(void)setTableHeaderView:(UIView *)tableHeaderView{
    [super setTableHeaderView:tableHeaderView];
    [self.norMessageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY).offset(-StatusBarAndNavH-tableHeaderView.frame.size.height);
    }];

}
-(void)beginRefreshing{
    [self.mj_header beginRefreshing];
}
-(void)reloadData{
    [super reloadData];
    [self endRefresh];
    NSInteger dataIndex = 0;
    for (NSInteger i = 0 ; i < self.numberOfSections; i ++ ) {
        dataIndex += [self numberOfRowsInSection:0];
    }
    if (dataIndex == 0 && self.isNoMoreView == YES) {
        self.norMessageView.hidden = NO;
        self.mj_footer.hidden = YES;
    }
    else{
        self.norMessageView.hidden = YES;
        self.mj_footer.hidden = NO;
    }
}
-(void)setIsAddHeader:(BOOL)isAddHeader{
    _isAddHeader = isAddHeader;
    if (isAddHeader == YES) {
        __weak typeof(self)weakSelf = self;
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf norRefData];
        }];
    }
    else{
        self.mj_header =(MJRefreshNormalHeader *)[[UIView alloc]init];
    }
}
-(void)setIsAddFooter:(BOOL)isAddFooter{
    _isAddFooter = isAddFooter;
    if (isAddFooter == YES) {
        __weak typeof(self)weakSelf = self;
        self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            if (weakSelf.mjLoadingMoreDataBlock) {
                weakSelf.mjLoadingMoreDataBlock();
            }
            if (weakSelf.mjDelegate && [weakSelf.mjDelegate respondsToSelector:@selector(MjRoadMoreAction)]) {
                [weakSelf.mjDelegate MjRoadMoreAction];
            }
        }];
    }
    else{
        self.mj_footer = (MJRefreshBackNormalFooter *)[[UIView alloc]init];
    }
}

-(void)endRefresh{
    if ([self.mj_header isKindOfClass:[MJRefreshNormalHeader class]]) {
        if (self.mj_header.isRefreshing) {
            [self.mj_header endRefreshing];
        }
    }
    if ([self.mj_footer isKindOfClass:[MJRefreshAutoNormalFooter class]]) {
        if (self.mj_footer.isRefreshing  && !self.isNoMoreData) {
            [self.mj_footer endRefreshing];
        }
    }

}
-(void)setIsNoMoreData:(BOOL)isNoMoreData{
    _isNoMoreData = isNoMoreData;
    if ([self.mj_footer isKindOfClass:[MJRefreshAutoNormalFooter class]]){
        if(isNoMoreData == YES){
            [self.mj_footer endRefreshingWithNoMoreData];
        }
        else{
            [self.mj_footer resetNoMoreData];
        }
    }
}

@end
